<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模型id" prop="modelId">
        <el-input
            v-model="queryParams.modelId"
            placeholder="请输入模型id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性id" prop="attrId">
        <el-input
            v-model="queryParams.attrId"
            placeholder="请输入属性id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性名称" prop="attrName">
        <el-input
            v-model="queryParams.attrName"
            placeholder="请输入属性名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级预警" prop="warnLevel1">
        <el-input
            v-model="queryParams.warnLevel1"
            placeholder="请输入一级预警"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['ba:baModel3WarnConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['ba:baModel3WarnConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['ba:baModel3WarnConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['ba:baModel3WarnConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="baModel3WarnConfigList" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="模型id" align="center" prop="modelId" v-if="false"/>
      <el-table-column label="属性id" align="center" prop="attrId" />
      <el-table-column label="属性名称" align="center" prop="attrName" />
      <el-table-column label="一级预警" align="center" prop="warnLevel1" />
      <el-table-column label="二级预警" align="center" prop="warnLevel2" />
      <el-table-column label="三级预警" align="center" prop="warnLevel3" v-if="false"/>
      <el-table-column label="是否有效" align="center" prop="valid">
        <template #default="scope">
          <dict-tag :options="valid" :value="scope.row.valid"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ba:baModel3WarnConfig:edit']">修改</el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ba:baModel3WarnConfig:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />-->

    <!-- 添加或修改崩岸模型3预警配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="baModel3WarnConfigRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型id" prop="modelId"  v-if="false">
          <el-input v-model="form.modelId" placeholder="请输入模型id"/>
        </el-form-item>
        <el-form-item label="属性名称" prop="attrId">
<!--          <el-input v-model="form.attrName" placeholder="请输入属性名称" />-->
          <el-select v-model="form.attrId" placeholder="请选择模型属性" style="width: 300px" @change="handleModelAttrChange" disabled>
            <el-option
                v-for="dict in ba_model3_attrs"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="属性id" prop="attrId" v-if="false">
          <el-input v-model="form.attrName" placeholder="请输入属性id" />
        </el-form-item>
        <el-form-item label="一级预警" prop="warnLevel1">
          <el-input v-model="form.warnLevel1" placeholder="请输入一级预警" />
        </el-form-item>
        <el-form-item label="二级预警" prop="warnLevel2">
          <el-input v-model="form.warnLevel2" placeholder="请输入二级预警" />
        </el-form-item>
        <el-form-item label="三级预警" prop="warnLevel3" v-if="false">
          <el-input v-model="form.warnLevel3" placeholder="请输入三级预警" />
        </el-form-item>
        <el-form-item label="是否有效" prop="valid">
          <el-select v-model="form.valid" placeholder="请选择是否有效">
            <el-option
                v-for="dict in valid"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BaModel3WarnConfig">
import { listBaModel3WarnConfig, getBaModel3WarnConfig, delBaModel3WarnConfig, addBaModel3WarnConfig, updateBaModel3WarnConfig } from "@/api/ba/baModel3WarnConfig";
// import {model3Info} from '@/utils/baCommon'
import {getModel3ByModelId} from '@/api/ba/baCommon'
const { proxy } = getCurrentInstance();
const { valid, ba_model3_attrs } = proxy.useDict('valid', 'ba_model3_attrs');



const baModel3WarnConfigList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const model3Info = ref({})

const baModel3Info = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    modelId: null,
    attrId: null,
    attrName: null,
    warnLevel1: null,
  },
  rules: {
    modelId: [
      { required: true, message: "模型id不能为空", trigger: "blur" }
    ],
    attrId: [
      { required: true, message: "属性id不能为空", trigger: "blur" }
    ],
    attrName: [
      { required: true, message: "属性名称不能为空", trigger: "blur" }
    ],
  }
});


const { queryParams, form, rules } = toRefs(data);

const props = defineProps({
  warnConfig: {
    type: Object,
    required: true,
    default: {
      id: -1,
      modelId: undefined,
    }
  }
})

/** 查询崩岸模型3预警配置列表 */
function getList() {
  loading.value = true;
  listBaModel3WarnConfig(queryParams.value).then(response => {
    baModel3WarnConfigList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    modelId: null,
    attrId: null,
    attrName: null,
    warnLevel1: null,
    warnLevel2: null,
    warnLevel3: null,
    valid: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("baModel3WarnConfigRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();

  form.value.modelId = model3Info.value.modelId
  form.value.modelName = model3Info.value.modelName
  form.value.valid = '1'
  open.value = true;
  title.value = "添加崩岸模型3预警配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBaModel3WarnConfig(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改崩岸模型3预警配置";
  });
}

function handleModelAttrChange(v) {
  // console.log(baModel3AttrsMap)
  // console.log(`${v}--${baModel3AttrsMap[v]}`)
}

/** 提交按钮 */
function submitForm() {
/*  proxy.$refs["baModel3WarnConfigRef"].validate(valid => {
    if (valid) {
      let l2 = toRaw(ba_model3_attrs.value)
      let f = false, attr = undefined;
      l2.forEach(item => {
        if (item.value === form.value.attrId) {
          f = true;
          attr = item
          form.value.attrName = item.label
        }
      })
      console.log(form.value)
    }
  });*/
  proxy.$refs["baModel3WarnConfigRef"].validate(valid => {
    if (valid) {
      let l2 = toRaw(ba_model3_attrs.value)
      let f = false, attr = undefined;
      l2.forEach(item => {
        if (item.value === form.value.attrId) {
          f = true;
          attr = item
          form.value.attrName = item.label
        }
      })

      if (form.value.id != null) {
        updateBaModel3WarnConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBaModel3WarnConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除崩岸模型3预警配置编号为"' + _ids + '"的数据项？').then(function() {
    return delBaModel3WarnConfig(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ba/baModel3WarnConfig/export', {
    ...queryParams.value
  }, `baModel3WarnConfig_${new Date().getTime()}.xlsx`)
}

// getList();

function init(){
  queryParams.value.modelId = props.warnConfig.modelId
  getModel3ByModelId(queryParams.value.modelId ).then(resp => {
    if (resp.code === 200) {
      model3Info.value = resp.data
    } else {
      model3Info.value = {}
    }
  })
  getList()
}

defineExpose({
  init
})
</script>
